<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="icon" th:href="@{/resources/favicon.ico}">
    <link rel="stylesheet" th:href="@{/resources/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/font/dtreefont.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/font/dtreefont.css}" media="all"/>
</head>
<body>
<input id="roleId" th:value="${roleId}" hidden>
<form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" class="layui-input input-radius"
                       placeholder="请输入用户名称">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit=""
                    lay-filter="doSearch"><i
                    class="layui-icon layui-icon-search layui-icon-normal"></i>查询
            </button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm"><i
                    class="layui-icon layui-icon-refresh"></i><span>重置</span>
            </button>
        </div>
    </div>
</form>
<!--数据表格-->
<div>
    <table class="layui-hide" id="userTable" lay-filter="userTable"></table>
    <div id="userToolBar" style="display: none">
        <button type="button" lay-event="add" shiro:hasPermission="sys:user:add"
                class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
            <i class="layui-icon layui-icon-add-1"></i>添加用户
        </button>
    </div>
    <div id="userRowBar" style="display: none;">
        </button>
        <button type="button" lay-event="delete" shiro:hasPermission="sys:role:delete"
                class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>取消授权
        </button>
    </div>
</div>
</body>
<script th:src="@{/resources/js/jquery-3.3.1.min.js}"></script>
<script th:src="@{/resources/layui/layui.js}"></script>
<script>
    var ctx = [[@{/}]];
    var roleId = $("#roleId").val();
    var deptTree;
    layui.extend({
        dtree: ctx + '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['dtree', 'layer', 'jquery', 'table', 'form'], function () {
        var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;

        //初始化表格
        var tableInsC = table.render({
            elem: "#userTable",
            title: "用户数据表格",
            url: ctx + "/role/selectGiveUserRole",
            toolbar: "#userToolBar",
            method: "post",
            page: true,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            response: {
                statusName: 'code', //状态字段名称
                statusCode: '0', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total',
                dataName: "rows"
            },
            where: {
                "id": roleId
            },
            height: "full-80",
            cols: [ [
                {field: 'name', title: '用户名称', align: 'center'},
                {field: 'address', title: '地址', align: 'center'},
                {
                    field: 'sex', title: '性别', align: 'center', templet: function (d) {
                        return d.sex == 1 ? '<font color="blue">男</font>' : '<font color="red">女</font>';
                    }
                },
                {
                    field: 'available', title: '是否可用', align: 'center', templet: function (d) {
                        return d.available == 1 ? '<font color="blue">可用</font>' : '<font color="red">不可用</font>';
                    }
                },
                {fixed: 'right', title: '操作', toolbar: '#userRowBar', align: 'center', width: 280}
            ] ],
            done: function (data, curr, count) {
                //不是第一页时，如果当前返回的数据为0那么就返回上一页
                if (data.data.length == 0 && curr != 1) {
                    tableInsC.reload({
                        page: {
                            curr: curr - 1
                        }
                    })
                }
            }
        });

        //点击重置按钮时，重置表单的值
        $("#resetRole").click(function () {
            $("#searchFrm")[0].reset();
        });

        //监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            tableInsC.reload({
                url:ctx + "/role/selectGiveUserRole",
                method: "post",
                where: {
                    "id": roleId,
                    "name": data.field.name
                },
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监控工具条事件
        table.on("toolbar(userTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    addUserRole(roleId);
                    break;
            };
        });

        //监控行工具条事件
        table.on("tool(userTable)",function (obj) {
            //获取当前行数据
            var data = obj.data;
            switch (obj.event){
                case 'delete':
                    deleteRole(data, roleId);
                    break;
            }
        });

        // 查询没有此角色的用户，并给新用户赋予此角色权限
        function addUserRole(roleId) {
            var mainIndex = layer.open({
                type: 2,
                content: ctx + '/role/toUsersNotThisRoleIndex/'+roleId,
                area: ['800px','95%'],
                title: '选择用户',
                btn: ['确定','关闭'],
                yes: function (index, layerOpen) {
                    var iframeWin = layerOpen.find('iframe')[0];
                    var users = iframeWin.contentWindow.getUsers();
                    if (users.size < 1){
                        layer.msg('请至少选择一条数据');
                    } else {
                        var userIds = users.map((user) => user.id);
                        var ids = userIds.toString();
                        $.post(ctx + '/role/insertUserRole', {ids: ids, roleId: roleId}, function (result) {
                            if (result.code == 0){
                                tableInsC.reload({
                                    where:"",
                                });
                            }
                            layer.msg(result.msg);
                        });
                        layer.close(index);
                    }
                }
            });
        }

        // 给此删除此用户的角色权限
        function deleteRole(data, roleId){
            layer.confirm('你确定要删除【'+data.name+'】这条数据吗？',{icon: 3,title:'提示'},function (index) {
                $.post(ctx + "/role/deleteRolesUser",{userId:data.id, roleId: roleId},function (res) {
                    if (res.code==0){
                        tableInsC.reload({
                            where:"",
                        });
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }
    });
</script>
</html>